package com.ibm.ba.demo.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ibm.ba.demo.entity.SPSSModelConfiguration;
import com.ibm.ba.demo.exception.GeneralException;
import com.ibm.ba.demo.helper.BeanHelper;
import com.spss.scoring.schema.v2.jaxb.Input;
import com.spss.scoring.schema.v2.jaxb.RequestInputRow;
import com.spss.scoring.schema.v2.jaxb.RequestInputTable;
import com.spss.scoring.schema.v2.jaxb.ScoreRequest;

@Service
public class ExDBScoreRequestServiceImpl implements
		ScoreRequestService {
	
	private static Logger logger = Logger.getLogger(ExDBScoreRequestServiceImpl.class);
	
	private ScoreRequest scoreRequest;
	
	@Autowired
	private SPSSModelConfiguration modelConfig;
	
	@PostConstruct  
	private void createScoreRequestHeader() {
		scoreRequest = new ScoreRequest();
		scoreRequest.setId(modelConfig.getSpssModel());
		RequestInputTable rInputTable = new RequestInputTable();
		rInputTable.setName(modelConfig.getSpssTable());
		scoreRequest.getRequestInputTable().add(rInputTable);
	}
	
	@Override
	public ScoreRequest convert(Map<String, Object> inputMap)
			throws GeneralException {
		logger.info("inputMap is:"
				+ BeanHelper.beanToJson(inputMap));
		RequestInputTable rInputTable = scoreRequest.getRequestInputTable().get(0);
		rInputTable.setName(modelConfig.getSpssTable());
		RequestInputRow rInputRow = new RequestInputRow();
		Input inputs = new Input();
		inputs.setName(modelConfig.getCustField());
		inputs.setValue(inputMap.get(modelConfig.getCustField()).toString());
		rInputRow.getInput().add(inputs);
		
		ArrayList<Object> intList = new ArrayList<Object>();
		  for(String key : inputMap.keySet()){
			  if (!key.equalsIgnoreCase(modelConfig.getCustField())) {
				  intList.add(inputMap.get(key));
			  }  
		  }
		
		List<String> productIdList = modelConfig.getStringList(modelConfig.getProductIds());

		for (int i=0;  i<productIdList.size(); i++) {
			inputs = new Input();
			inputs.setName(modelConfig.getSpssProductPrefix()+productIdList.get(i));
			if (intList.contains(i)) {
				inputs.setValue(modelConfig.getSpssProdTrueValue());
			} else {
				inputs.setValue(modelConfig.getSpssProdDefaultValue());
			}
			rInputRow.getInput().add(inputs);
		}
		rInputTable.getRequestInputRow().add(rInputRow);
		//scoreRequest.getRequestInputTable().add(rInputTable);
		logger.info("scoreRequest is:"
					+ BeanHelper.beanToJson(scoreRequest));
		return scoreRequest;
	}
	
	public ScoreRequest convert(List<Map<String, Object>> inputList)
			throws GeneralException {
		logger.info("inputList is:"
				+ BeanHelper.beanToJson(inputList));
		scoreRequest.setId(modelConfig.getSpssModel());
		RequestInputTable rInputTable = new RequestInputTable();
		rInputTable.setName(modelConfig.getSpssTable());

		for (int i=0; i<inputList.size();i++) {
			convert(inputList.get(i));
		}
		
		logger.info("scoreRequest is:"
					+ BeanHelper.beanToJson(scoreRequest));
		return scoreRequest;
	}

}
